//
//  FLLAutoLayout.m
//  FLLGodOfCooking
//
//  Created by LL.F on 16/9/13.
//  Copyright © 2016年 LL.F. All rights reserved.
//

#import "FLLAutoLayout.h"

@interface FLLAutoLayout ()
@property (nonatomic, strong) NSMutableArray<UICollectionViewLayoutAttributes *> *attributesArray;
@property (nonatomic, assign) CGFloat allWidth;
@property (nonatomic, assign) CGFloat cellMargin;
@end
@implementation FLLAutoLayout
// 初始化
- (instancetype)init
{
    self = [super init];
    if (self) {
        _attributesArray = [NSMutableArray array];
    }
    return self;
}
// 使用Xib 走这个初始化
- (void)awakeFromNib
{
    
    _attributesArray = [NSMutableArray array];
    
}
//
- (CGFloat)cellMargin {
    _cellMargin = CGRectGetHeight(self.collectionView.frame) / 10;
    return _cellMargin;
}
//
- (void)prepareLayout {
    [self.attributesArray removeAllObjects];
    self.allWidth = 0;
    for (NSInteger i = 0; i < [self.collectionView numberOfItemsInSection:0]; i++) {
        NSIndexPath *index = [NSIndexPath indexPathForItem:i inSection:0];
        UICollectionViewLayoutAttributes *attribute = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:index];
        CGFloat x = _allWidth + self.cellMargin;
        CGFloat y = self.cellMargin / 2.0;
        CGFloat height = CGRectGetHeight(self.collectionView.frame) / 4.5 * 5;

        CGFloat width = [self.delegate collectionView:self.collectionView layout:self height:height widthForItemAtIndexPath:index];
        _allWidth += width + self.cellMargin;
        attribute.frame = CGRectMake(x, y, width, height);
        [_attributesArray addObject:attribute];
        
        
    }
}
- (NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect {
    return _attributesArray;
}
// 负责collectionView的滚动范围
- (CGSize)collectionViewContentSize {
    return CGSizeMake(_allWidth, 0);
}

@end
